﻿using BCCommon;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Configs
{
    public static class MySqlExtension
    {
        public static IServiceCollection AddMySql(this IServiceCollection services, Action<MySqlOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.AddOptions();
            services.Configure(setupAction);

            return services.AddScoped<IDatabaseContext, MySqlContext>(p =>
            {
                var options = p.GetService<IOptions<MySqlOptions>>();

                return new MySqlContext(options.Value);
            });
        }
    }
}
